<?php
/**
 *   
 * ds
 * 2020.08.13
 */
namespace App\Models\MainDB;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class GiftBoxPrizeDetail extends Model{

    protected $table = 'ln_gift_box_prize_detail';     //数据库内完整表名

    protected $primaryKey = 'id';    //主键名

    protected $fillable = [
        'id',      // 主键id
        'gift_id',  //礼物id
        'type',         // 类型：1银盒 2金盒
        'number',         // 获得奖品的个数
        'gold_coin',         // 该奖品的金币数量
        'user_id',//送礼用户id
        'add_time', // 时间
        'given_user_id',  //手里人的用户id
    ];
    public $timestamps = false;
    public function  gift(){
        return $this->hasOne('App\Models\MainDB\Gift','id','gift_id');
    }

    public function getListPaging($where, $keyword, $fields = '*', $perPage = 10)
    {
        $data = $this->from('ln_gift_box_prize_detail as gbpd')
            ->select($fields)
            ->leftJoin('ln_user_data as ud', 'gbpd.given_user_id', '=', 'ud.user_id')
            ->leftJoin('ln_gift as g', 'gbpd.gift_id', '=', 'g.id')
            ->where($where)
            ->where(function ($query) use ($keyword) {
                if ($keyword) {
                    $query->where('ud.user_code', $keyword)
                        ->orWhere('ud.nickname', $keyword);
                }
            })
            ->orderBy('gbpd.add_time', 'desc')
            ->paginate($perPage);
        return $data;
    }
}